package com.chb.queue;

import java.util.Stack;

/**
 * 使用 两个栈 模拟的队列
 */
public class StackMockQueue {
    private Stack<Integer> inStack = new Stack<Integer>();
    private Stack<Integer> outStack = new Stack<Integer>();
    public StackMockQueue() {}
    public void push(int x) {
        inStack.push(x);
    }

    public int pop() {
        if (inStack.empty()) {
            throw new RuntimeException("队列空");
        }
        while(!inStack.empty()) {
            outStack.push(inStack.pop());
        }
        int val = outStack.pop();
        while(!outStack.empty()) {
            inStack.push(outStack.pop());
        }
        return val;
    }

    public int peek() {
        if (inStack.empty()) {
            throw new RuntimeException("队列空");
        }
        while(!inStack.empty()) {
            outStack.push(inStack.pop());
        }
        int val = outStack.peek();
        while(!outStack.empty()) {
            inStack.push(outStack.pop());
        }
        return val;
    }

    public boolean empty() {
        return inStack.empty();
    }

}
